/*The MIT License

Copyright (c) <2008> <Samir Menon>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#ifndef CSPIKESPOOLER_H_
#define CSPIKESPOOLER_H_

#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

#include "sys/time.h"
#include "neurontools.h"
#include "CLogger.h"

using namespace std;

namespace bis_neuron
{
	

struct Spike_rec_struct	{//Records spike info
public:
	int neuron_id;	
	timeval t_spike;
	void set( int arg_neuron_id){
		neuron_id = arg_neuron_id;		
		gettimeofday(&t_spike,NULL);
	}			
};

class CSpikeSpooler : public CLogger
{	
	//Inherited:
//	string logfile_name;
//	bool write_out_flag;
//	std::vector< string > log_vector;
//	ofstream log_file;
	Spike_rec_struct spike_spool;		
	timeval t_start, t_diff;
	stringstream log_stream;
				//TODO: Implement a doubly linked list of the 
				//spikes of each neuron in the vector of all spikes
				//CNode<Spike_rec_struct> spike_spool;
				//std::vector< CNode<Spike_rec_struct> > spike_vector;
				//CDoublyLinkedList<Spike_rec_struct*> spike_list;
	void compute_t_diff();	
	string log_dump;
public:	
	CSpikeSpooler(string arg_outfilename="");
	virtual ~CSpikeSpooler();
	
	void spool_spike(int arg_neuron_id);
	void spool_data(int arg_neuron_id, string *log_data);
	//Inherited:
//	void log(string arg_log);
//	bool flush_logs();
	
	//Wrappers
	inline void set_spool_flag_on() { set_writeout_flag_on();}
	inline void set_spool_flag_off() { set_writeout_flag_off();}
	inline bool get_spool_flag() { return get_writeout_flag();}
};

}

#endif /*CSPIKESPOOLER_H_*/
